package com.hotel.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hotel.mapper.GoodsMapper;
import com.hotel.pojo.Goods;
import com.hotel.service.GoodsService;
import com.hotel.utils.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: JiangMingda
 * File: GoodsServiceImpl
 * Time: 2022/8/17 17:21
 * Description: 业务逻辑实现类
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    // 装配GoodsMapper
    @Autowired
    private GoodsMapper goodsMapper;

    // 查询商品  pageHelper分页
    @Override
    public ResultInfo selectGoods(String goodsName, int page, int size) {
        // 开启分页
        PageHelper.startPage(page, size);
        // 查询
        List<Goods> goodsList = goodsMapper.selectAllGoods(goodsName);
        // 将结果封装到pageInfo对象中
        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goodsList);
        return new ResultInfo(200, "查询商品", goodsPageInfo);
    }

    // 删除商品
    @Override
    public ResultInfo DeleteGoodsByGoodsId(Integer goodsId) {
        Integer deleteGoods = goodsMapper.deleteGoodsByGoodsId(goodsId);
        if (deleteGoods > 0){
            return new ResultInfo(200,"删除成功！");
        }
        return new ResultInfo(500,"删除失败！");
    }

    // 修改商品
    @Override
    public ResultInfo updateGoodsByGoodsName(Goods goods) {
        Integer updateGoods = goodsMapper.updateGoods(goods);
        if (updateGoods > 0) {
            return new ResultInfo(200, "修改成功！");
        }
        return new ResultInfo(500, "修改失败！");
    }

    // 新增商品
    @Override
    public ResultInfo insertGoods(Goods goods) {
        Integer insertGoods = goodsMapper.insertGoods(goods);
        if (insertGoods > 0) {
            return new ResultInfo(200, "新增成功！");
        }
        return new ResultInfo(500, "咱hotel已经有" + goods.getGoodsName() +"这个商品了哟！");
    }

    // 使用情况可视化
    @Override
    public ResultInfo goodsVisual() {
        // 定义空list存储玫瑰图所需数据
        List<Map<String,Object>> goodsList = new ArrayList<>();
        // 查询到商品名称及对应的价格
        // [Goods(goodsId=null, goodsName=纸巾, goodsNumber=200, goodsPrice=null), Goods(goodsId=null, goodsName=吹风机, goodsNumber=200, goodsPrice=null)]
        List<Goods> goodsVisual = goodsMapper.goodsVisual();
        // foreach遍历godsVisual进List，  南丁格尔图所需的参数类型：List<Map<String,Object>>
        for (Goods goods :
                goodsVisual) {
            // 定义空map存储玫瑰图所需数据
            Map<String,Object> goodsMap = new HashMap<>();
            // 内层循环保证一个map对应一个商品和价格
            for (int i = 0; i < 1; i++) {
                goodsMap.put("value", goods.getGoodsNumber());
                goodsMap.put("name", goods.getGoodsName());
            }
            goodsList.add(goodsMap);
        }
        return new ResultInfo(200, "可视化分析！", goodsList);
    }
}
